/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.luedinski.objects;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import com.luedinski.utilities.ConnectionPooling;

/**
 * 
 * @author lueder
 */
public class Station {

	static final double EARTHS_RADIUS = 6371; // km
	private int id;
	private String name;
	private double lat;
	private double lng;

	// HashSet<Station> childStations = new HashSet<Station>();

	public Station(int id, String name, double lat, double lng) {
		this.id = id;
		this.name = name;
		this.lat = lat;
		this.lng = lng;

	}

	@Override
	public String toString() {
		return id + ", " + name;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public double getX() {
		return lat;
	}

	public double getY() {
		return lng;
	}

	public static double computeGeoDistance(Station s1, Station s2) {
		if (s1 == null || s2 == null) {
			return 0.0;
		} else {
			double dLat = Math.toRadians(s2.lat - s1.lat);
			double dLon = Math.toRadians(s2.lng - s1.lng);
			double lat1 = Math.toRadians(s1.lat);
			double lat2 = Math.toRadians(s2.lat);

			double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
					+ Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1)
					* Math.cos(lat2);
			double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
			return EARTHS_RADIUS * c;
		}
	}

	public boolean equals(Station s) {
		return this.id == s.id ? true : false;
	}
	
	public static Station fromSql(int id){
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		String sql = "SELECT id, name, lat, lng FROM station WHERE id="+id;

		try {
			conn = ConnectionPooling.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			
			rs.next();
			return new Station(rs.getInt(1), rs.getString(2), rs
					.getDouble(3), rs.getDouble(4));

		} catch (Exception e) {
			return null;
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (Exception e) {
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
				}
			if (conn != null)
				try {
					conn.close();
				} catch (Exception e) {
				}
		}
		
	}

	public static ArrayList<Station> findStations(String search) {
		ArrayList<Station> stations = new ArrayList<Station>();

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		String sql = "SELECT id, name, lat, lng FROM station WHERE name LIKE '%"
				+ search + "%' LIMIT 21";

		try {
			conn = ConnectionPooling.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			
			while (rs.next()) {
				stations.add(new Station(rs.getInt(1), rs.getString(2), rs
						.getDouble(3), rs.getDouble(4)));
			}
			return stations;

		} catch (Exception e) {
			return null;
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (Exception e) {
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
				}
			if (conn != null)
				try {
					conn.close();
				} catch (Exception e) {
				}
		}
	}
}
